﻿@model IEnumerable<CubicFWNet.Data.Models.User>
@{
    ViewBag.Title = "用户管理";
}
<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        用户管理
    </h1>
    <ol class="breadcrumb">
        <li><a href="javascript:void(0);"><i class="fa fa-dashboard"></i>@Resources.Global.PageTitle</a></li>
        <li><a href="@Url.Action("Index","Home")">首页</a></li>
        <li class="active"><a href="@Url.Action("Index","User")">用户管理</a></li>
    </ol>
</section>
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="box box-solid">
                <div class="box-nav clearfix">
                    <div class="col-sm-12 col-md-9">
                        <div class="col-sm-12 col-md-3">
                            <input id="filter" type="text" class="form-control" placeholder="搜索关键字">
                        </div>
                        <button type="button" class="btn btn-primary" onclick="create();" title="新建"><i class="fa fa-plus-square"></i>&nbsp;新建用户</button>
                        @*<button type="button" class="btn btn-warning" oncancel="edit();" title="修改"><i class="fa fa-edit"></i></button>
        <button type="button" class="btn btn-danger" onclick="del()" title="删除"><i class="fa fa-trash-o"></i></button>*@
                        @*<button type="button" class="btn btn-info" title="查看"><i class="fa fa-search-plus"></i></button>*@
                        <a class="btn btn-default" onclick="$.reload()" title="刷新"><span class="glyphicon glyphicon-refresh"></span></a>
                    </div>
                </div>
                <div class="box-body">
                    <table class="footable table table-stripped table-bordered table-hover" id="footable" data-page-size="20" data-filter=#filter>
                        <thead>
                            <tr>
                                <th data-sort-ignore="true">
                                    @Html.DisplayNameFor(model => model.RealName)
                                </th>
                                <th data-sort-ignore="true">
                                    @Html.DisplayNameFor(model => model.UserName)
                                </th>
                                <th data-hide="phone,tablet" data-sort-ignore="true">
                                    @Html.DisplayNameFor(model => model.CreateTime)
                                </th>
                                <th data-sort-ignore="true">
                                    @Html.DisplayNameFor(model => model.SortNo)
                                </th>
                                <th data-sort-ignore="true">
                                    @Html.DisplayNameFor(model => model.Status)
                                </th>
                                <th data-hide="phone,tablet" data-sort-ignore="true">
                                    @Html.DisplayNameFor(model => model.Memo)
                                </th>
                                <th data-hide="phone,tablet" data-sort-ignore="true">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            @if (null != Model)
                            {
                                foreach (var item in Model)
                                {
                                    <tr>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.RealName)
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.UserName)
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.CreateTime)
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.SortNo)
                                        </td>
                                        <td>
                                            @if (item.Status)
                                            {
                                                <span class="btn-xs bg-green">正常</span>
                                            }
                                            else
                                            {
                                                <span class="btn-xs bg-black">禁用</span>
                                            }
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.Memo)
                                        </td>
                                        <td>
                                            <button type="button" onclick="resetPwd('@item.Id')" class="btn btn-xs btn-info btn-flat">@*<i class="fa fa-search"></i>*@重置密码</button>
                                            <button type="button" onclick="edit('@item.Id');" class="btn btn-xs bg-blue">编辑</button>
                                            @*<button type="button" class="btn  btn-xs btn-info btn-flat"><i class="fa fa-search"></i></button>*@
                                            <button type="button" onclick="del(this, '@item.Id');" class="btn  btn-xs bg-orange">删除</button>
                                        </td>
                                    </tr>
                                }
                            }
                        </tbody>
                        <tfoot>
                            <tr>
                                <td colspan="7">
                                    <ul class="pagination1 pagination pull-right"></ul>
                                </td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
</section>
<!--编辑对话框-->
<div class="modal" id="editDlg" style="display: none">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">添加/修改用户</h4>
            </div>
            <form class="form-horizontal" id="frmedit" action="@Url.Action("Add", "User")" method="post">
                <div class="modal-body">
                    <div class="nav-tabs-custom" style="margin-bottom: 0; box-shadow: none;">
                        <ul class="nav nav-tabs">
                            <li class="active"><a href="#tab_1" data-toggle="tab" aria-expanded="false">基本信息</a></li>
                            @*<li class=""><a href="#tab_2" data-toggle="tab" aria-expanded="true">分配岗位</a></li>*@
                            <li class=""><a href="#tab_3" data-toggle="tab" aria-expanded="true">分配权限</a></li>
                        </ul>
                        <div class="tab-content">
                            <div class="tab-pane active" id="tab_1">
                                <div class="form-group">
                                    <input type="hidden" name="Id" class="form-control formId" id="Id" />
                                    <label class="col-sm-3 control-label no-padding-right" for=""> 账号 </label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" id="UserName" name="UserName" required />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label no-padding-right" for=""> 姓名 </label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" id="RealName" name="RealName" required />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label no-padding-right" for=""> 用户编号 </label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" id="ShortName" name="ShortName" required />
                                    </div>
                                </div>
                                <div class="form-group" id="divPwd">
                                    <label class="col-sm-3 control-label no-padding-right" for="">密码</label>
                                    <div class="col-sm-8">
                                        <input type="password" class="form-control" id="Password" name="Password" />
                                        <span class="help-block">默认与账户相同</span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label no-padding-right" for=""> 排序号 </label>
                                    <div class="col-sm-8">
                                        <input type="number" class="form-control" id="SortNo" name="SortNo" value="0" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label no-padding-right" for=""> 状态 </label>
                                    <label class="col-sm-8" style="padding-top: 5px;">
                                        <input type="checkbox" name="Status" id="Status" checked>
                                    </label>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label no-padding-right" for=""> 备注 </label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" id="Memo" name="Memo" />
                                    </div>
                                </div>
                            </div>
                            @*<div class="tab-pane" id="tab_2">
                                    <div id="permissionTree"></div>
                                </div>*@
                            <div class="tab-pane" id="tab_3">
                                <div id="moduleTree"></div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default pull-left" data-dismiss="modal">关闭</button>
                    <button type="submit" class="btn btn-primary pull-right">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>
<div class="modal" id="editDlg_pwd" style="display: none">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">重置密码</h4>
            </div>
            <form class="form-horizontal" id="frmedit_pwd" action="@Url.Action("UpdateUserPwd","User")" method="post">
                <div class="modal-body">
                    <div class="nav-tabs-custom" style="margin-bottom:0;box-shadow:none;">
                        <div class="tab-content">
                            <div class="tab-pane active" id="tab_pwd">
                                <div class="form-group">
                                    <input type="hidden" name="id" class="form-control formId" id="id" />
                                    <label class="col-sm-3 control-label no-padding-right" for=""> 密码 </label>
                                    <div class="col-sm-8">
                                        <input type="password" class="form-control" id="newPassword" name="newPassword" required />
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default pull-left" data-dismiss="modal">关闭</button>
                    <button type="submit" class="btn btn-primary pull-right">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>
<div id="loadingPage" style="display: none;">
    <div class="loading-shade"></div>
    <div class="loading-content" onclick="$.loading(false)">数据加载中，请稍后…</div>
</div>
@*@section Styles {
        @Styles.Render("~/Content/plugins/dataTables/dataTablesStyles")
    }*@
@section Scripts {
    <script src="~/Scripts/md5/jquery.md5.js"></script>
    <script>
        $(document).ready(function () {
            $('#footable').footable();
            $("#frmedit").validate({
                submitHandler: function (form) {
                    var postData = $(form).formSerialize();
                    //postData["roleIds"] = String($("#permissionTree").getCheckedNodes());
                    postData["moduleIds"] = String($("#moduleTree").getCheckedNodes());
                    if (!$.trim($("#Password").val())) {
                        postData["Password"] = $.md5($.trim($("#UserName").val()));
                        postData["IsPwd"] = 0;
                    } else {
                        postData["Password"] = $.md5($.trim($("#Password").val()));
                        postData["IsPwd"] = 1;
                    }
                    postData["IsPwd"] = 1;
                    //postData.push({
                    //    "name": "roleIds",
                    //    "value": String($("#permissionTree").getCheckedNodes())
                    //});
                    $.submitForm({
                        url: $(form).attr("action"),
                        param: postData,
                        success: function () {
                            $.reload();
                        }
                    });
                }
            });

            $("#frmedit_pwd").validate({
                submitHandler: function (form) {
                    var postData = $(form).formSerialize();
                    postData["newPassword"] = $.md5($.trim($("#newPassword").val()));
                    postData["IsPwd"] = 1;
                    $.submitForm({
                        url: $(form).attr("action"),
                        param: postData,
                        success: function () {
                            $.reload();
                        }
                    });
                }
            });
        });

        function initControl(id) {
            if (!id || typeof id == undefined) {
                id = '00000000-0000-0000-0000-000000000000';
            }
            //$("#permissionTree").treeview({
            //    height: 300,
            //    showcheck: true,
            //    url: "/User/GetRoles",
            //    param: { userId: id }
            //});
            $("#moduleTree").treeview({
                height: 300,
                showcheck: true,
                url: "/User/GetModuleTree",
                param: { userId: id }
            });
        }
        function resetPwd(id) {
            $("#frmedit_pwd").formInit();
            $('#editDlg_pwd').modal();
            $('#tab_pwd').find("#id").val(id);
        }

        function create() {
            initControl();
            $("#divPwd").show();
            $("#frmedit").formInit();
            $('#editDlg').modal();
        }

        function edit(id) {
            if (!!id) {
                $("#divPwd").hide();
                initControl(id);
                $.ajax({
                    url: "/User/GetFormJson",
                    data: { id: id },
                    dataType: "json",
                    async: false,
                    success: function (data) {
                        $("#frmedit").formSerialize(data);
                        $("#editDlg").modal();
                    }
                });
            }
        }

        function del(obj, id) {
            if (!!id) {
                $.deleteForm({
                    url: "/User/Delete",
                    param: { id: id },
                    success: function () {
                        $(obj).parents("tr").remove();
                    }
                });
            }
        }
    </script>
}


